﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using Tradukisto.Engine.Utilities;

namespace Tradukisto.Engine.ResourceProviders
{
    public class LocalizationDbProvider : LocalizationProviderBase
    {
        private string _connectionString;

        public LocalizationDbProvider()
            : this(ConfigurationManager.ConnectionStrings["LocalizationConnectionString"].ConnectionString)
        {
        }

        public LocalizationDbProvider(string connectionStringName)
            : base()
        {
            _connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
        }

        protected override string OnGetString(string cultureName, string name)
        {
            try
            {

                using (var conn = new SqlConnection(_connectionString))
                {
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "SELECT Value FROM LocalItem WHERE Name = @name AND Culture = @culture";
                        cmd.Parameters.AddWithValue("name", name);
                        cmd.Parameters.AddWithValue("culture", cultureName);
                        conn.Open();
                        var value = cmd.ExecuteScalar();
                        return (string)value;
                    }
                }
            }
            catch (Exception e)
            {
                var message = string.Format("Erro while reading {0} and {1} from database.\n{2}", name, cultureName, e.ToString());
                SimpleLogger.WriteError(message);
                throw;
            }
        }

        public override Dictionary<string, string> GetAllStringsByControllerAndAction(string culture, string controller, string action)
        {
            try
            {

                using (var conn = new SqlConnection(_connectionString))
                {
                    using (var cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "SELECT Name, Value FROM LocalItem WHERE Controller = @controller AND Action = @action AND Culture = @culture";
                        cmd.Parameters.AddWithValue("controller", controller);
                        cmd.Parameters.AddWithValue("action", action);
                        cmd.Parameters.AddWithValue("culture", culture);
                        conn.Open();
                        var values = cmd.ExecuteReader();

                        var ret = new Dictionary<string, string>(0);
                        while (values.Read())
                        {
                            ret.Add(values[0].ToString(), values[1].ToString());
                        }
                        values.Close();
                        values.Dispose();
                        return ret;
                    }
                }
            }
            catch (Exception e)
            {
                var message = string.Format("Erro while reading all strings from {0}, {1} and {2} from database.\n{3}", controller, action, culture, e.ToString());
                SimpleLogger.WriteError(message);
                throw;
            }
        }

        public override Dictionary<string, string> GetAllStringsByControllerAndAction(string controller, string action)
        {
            return GetAllStringsByControllerAndAction(LocalizationProvider.CultureName, controller, action);
        }
    }
}